package hander

import (
	"dev-manage-sys/cache"
	"dev-manage-sys/model"
	"dev-manage-sys/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

//	func UserList(c *gin.Context) {
//		var article []model.User
//		model.DB.Find(&article)
//		c.JSON(200, gin.H{
//			"code": 200,
//			"msg":  "查询成功",
//			"data": article,
//		})
//	}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var article []model.User
	redis_key := "list_" + strconv.Itoa(page)
	createData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if createData == "" {
		model.DB.Offset(offset).Limit(size).Find(&article)
		jsonData, _ := json.Marshal(article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(createData), &article)
	}

	var total int64
	model.DB.Model(model.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  article,
		"total": total,
	})
	return
}
func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	redis_key := "login" + username
	createData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if createData >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误三次，禁止登入",
		})
		return
	}

	var user model.User
	err := model.DB.Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "账号密码不正确",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if createData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "密码错误三次，禁止登入两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "登入失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登入成功",
	})
	return
}
func RoleCreate(c *gin.Context) {
	rolename := c.PostForm("rolename")

	data := model.Role{

		RoleName: rolename,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func NodeCreate(c *gin.Context) {
	nodecreate := c.PostForm("nodecreate")

	data := model.Node{
		NodeName: nodecreate,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	redis_key := "node_create" + strconv.Itoa(int(data.ID))
	createData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if createData == "" {
		model.DB.Where("id= ?", data.ID).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func UserRole(c *gin.Context) {
	//SELECT
	//*
	//	FROM
	//
	//roles
	//
	//LEFT JOIN user_roles ON roles.id = user_roles.rid
	//WHERE user_roles.rid = 1
	rid, _ := strconv.Atoi(c.Query("rid"))

	var data []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.rid = ?", rid).
		Debug().
		Find(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
	return
}
func RoleNode(c *gin.Context) {
	//SELECT
	//*
	//	FROM
	//nodes
	//LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
	//WHERE role_nodes.nid=1
	nid, _ := strconv.Atoi(c.Query("nid"))

	var node model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.nid= ?", nid).
		Debug().
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func GoodCreate(c *gin.Context) {
	goodname := c.PostForm("goodname")

	data := model.Good{
		GoodName: goodname,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrangeCreate(c *gin.Context) {
	orangeno := c.PostForm("orangeno")
	orangename := c.PostForm("orangename")
	orangeprice := c.PostForm("orangeprice")

	data := model.Orange{

		OrangeNo:    orangeno,
		OrangeName:  orangename,
		OrangePrice: orangeprice,
	}

	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrangeList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var article []model.Orange
	redis_key := "orange_list" + strconv.Itoa(page)
	createDat, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if createDat == "" {
		model.DB.Offset(offset).Limit(size).Find(&article)
		jsonData, _ := json.Marshal(article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(createDat), &article)
	}
	var total int64
	model.DB.Model(model.Orange{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  article,
		"total": total,
	})
	return

}
func ListOrange(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
}
